<!DOCTYPE html>
<html lang="en">
    <head prefix="og: https://ogp.me/ns#">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="color-scheme" content="light dark">
  
  <title>day04-Maven - Satellite</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    <link rel='manifest' href='/manifest.json'>
  

  
  
  
  <meta property="og:title" content="day04-Maven - Satellite" />
  
  <meta property="og:type" content="article" />
  
  <meta property="og:url" content="http://example.com/2023/09/21/day04-Maven/index.html" />
  
  <meta property="og:image" content="/favicon.png" />
  
  <meta property="og:article:published_time" content="2023-09-21T02:09:06.000Z" />
  
  <meta property="og:article:author" content="John Doe" />
  
  

  
<link rel="stylesheet" href="/css/var.css">

  
<link rel="stylesheet" href="/css/main.css">

  
<link rel="stylesheet" href="/css/typography.css">

  
<link rel="stylesheet" href="/css/code-highlighting.css">

  
<link rel="stylesheet" href="/css/components.css">

  
<link rel="stylesheet" href="/css/nav.css">

  
<link rel="stylesheet" href="/css/paginator.css">

  
<link rel="stylesheet" href="/css/footer.css">

  
<link rel="stylesheet" href="/css/post-list.css">

  
  
<link rel="stylesheet" href="/css/rainbow-banner.css">

  
  
  
<link rel="stylesheet" href="/css/toc.css">

  
  
  
  
  
<link rel="stylesheet" href="/css/post.css">

  
  
  
  
  

  
<meta name="generator" content="Hexo 6.3.0"></head>
    <body
        data-color-scheme="auto"
        data-uppercase-categories="true"
        
        data-rainbow-banner="true"
        data-rainbow-banner-shown="auto"
        data-rainbow-banner-month="6"
        data-rainbow-banner-colors="#e50000,#ff8d00,#ffee00,#008121,#004cff,#760188"
        
        data-config-root="/"
        
        data-toc="true"
        data-toc-max-depth="2"
        
        
    >
        <nav id="theme-nav">
    <div class="inner">
        <a class="title" href="/">Satellite&#39;s Blog</a>
        <div class="nav-arrow"></div>
        <div class="nav-items">
            <a class="nav-item nav-item-home" href="/">Home</a>
            
            
            <a class="nav-item" href="/archives">Archives</a>
            
            
            
            <a class="nav-item" href="/about">About</a>
            
            
            
            <a class="nav-item nav-item-github nav-item-icon" href="https://github.com/Satellite2426" target="_blank" aria-label="GitHub">&nbsp;</a>
            
            
        </div>
    </div>
</nav>
        
<article class="post">
    <div class="meta">
        

        
        <div class="date" id="date">
            <span>September</span>
            <span>21,</span>
            <span>2023</span>
        </div>
        

        <h2 class="title">day04-Maven</h2>
    </div>

    <div class="divider"></div>

    <div class="content">
        <h1 id="1-Maven简介"><a href="#1-Maven简介" class="headerlink" title="1. Maven简介"></a>1. Maven简介</h1><p><strong>Maven</strong>是专门用于管理和构建Java项目的工具，它的主要功能有：</p>
<ul>
<li>提供了一套标准化的项目结构    （标准化的项目结构）</li>
<li>提供了一套标准化的构建流程(编译，测试，打包，发布……)      （标准化的构建流程）</li>
<li>提供了一套依赖管理机制      （方便的依赖管理 ）</li>
</ul>
<blockquote>
<p>Maven提供了一套标准化的项目结构，所有IDE使用Maven构建的项目结构完全一样，所有IDE创建的Maven项目可以通用</p>
</blockquote>
<p><img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309081723279.png" alt="image-20230908172306152"></p>
<p>1.Maven 使用标准的<strong>坐标</strong>配置来管理各种依赖</p>
<p>2.只需要简单的配置就可以完成依赖管理</p>
<p><strong>仓库分类</strong>：</p>
<ul>
<li>本地仓库：自己计算机上的一个目录</li>
<li>中央仓库：由Maven团队维护的全球唯一的仓库地: <a target="_blank" rel="noopener" href="https://repo1.maven.org/maven2/">https://repo1.maven.org/maven2/</a></li>
<li>远程仓库(私服)：一般由公司团队搭建的私有仓库</li>
</ul>
<p><strong>当项目中使用坐标引入对应依赖jar包后，首先会查找本地仓库中是否有对应的jar包</strong>：</p>
<ul>
<li>如果有，则在项目直接引用；</li>
<li>如果没有，则去中央仓库中下载对应的jar包到本地仓库</li>
</ul>
<p><strong>还可以搭建远程仓库，将来jar包的查找顺序则变为</strong>：</p>
<p>本地仓库 -&gt; 远程食库 -&gt; 中央仓库</p>
<h1 id="2-Maven的安装和配置"><a href="#2-Maven的安装和配置" class="headerlink" title="2. Maven的安装和配置"></a>2. Maven的安装和配置</h1><h2 id="2-1-Maven安装配置"><a href="#2-1-Maven安装配置" class="headerlink" title="2.1 Maven安装配置"></a>2.1 Maven安装配置</h2><ol>
<li>解压apache-maven-3.6.1rar既安装完成</li>
<li>配置环境变量MAVEN HOME为安装路径的bin目录</li>
<li>配置本地仓库:修改conf&#x2F;settings.xml中的 <localRepository> 为一个指定目录</li>
<li>配置阿里云私服:修改 conf&#x2F;settings.xml中的<mirrors>标签，为其添加如下子标签</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">id</span>&gt;</span>alimaven<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">name</span>&gt;</span>aliyun maven<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">url</span>&gt;</span>http://maven.aliyun.com/nexus/content/groups/public/<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="2-2-Maven基本使用"><a href="#2-2-Maven基本使用" class="headerlink" title="2.2 Maven基本使用"></a>2.2 Maven基本使用</h2><h3 id="2-2-1-Maven常用命令"><a href="#2-2-1-Maven常用命令" class="headerlink" title="2.2.1 Maven常用命令"></a>2.2.1 Maven常用命令</h3><ul>
<li>compile：编译</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn compile</span><br></pre></td></tr></table></figure>

<ul>
<li>clean：清理</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn clean</span><br></pre></td></tr></table></figure>

<ul>
<li>test：测试</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn test</span><br></pre></td></tr></table></figure>

<ul>
<li>package：打包</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn <span class="keyword">package</span></span><br></pre></td></tr></table></figure>

<ul>
<li>install：安装</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn install</span><br></pre></td></tr></table></figure>

<h3 id="2-2-2-Maven生命周期"><a href="#2-2-2-Maven生命周期" class="headerlink" title="2.2.2 Maven生命周期"></a>2.2.2 Maven生命周期</h3><p>1.Maven构建项目生命周期描述的是一次构建过程经历经历了多少个事件</p>
<p>2.Maven 对项目构建的生命周期划分为3套</p>
<ul>
<li>clean：清理工作</li>
<li>default：核心工作，例如编译，测试，打包，安装等</li>
<li>site：产生报告，发布站点等</li>
</ul>
<p><img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309090905522.png" alt="image-20230909090540463"></p>
<h1 id="3-IDEA配置Maven"><a href="#3-IDEA配置Maven" class="headerlink" title="3. IDEA配置Maven"></a>3. IDEA配置Maven</h1><h2 id="3-1-IDEA配置Maven环境"><a href="#3-1-IDEA配置Maven环境" class="headerlink" title="3.1 IDEA配置Maven环境"></a>3.1 IDEA配置Maven环境</h2><ol>
<li>选择IDEA中 File –&gt; Settings</li>
</ol>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309090912819.png" alt="image-20230909091208791" style="zoom:80%;" />

<ol start="2">
<li>搜索maven</li>
</ol>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309090912002.png" alt="image-20230909091242976" style="zoom:80%;" />

<ol start="3">
<li>设置IDEA使用本地安装的 Maven，并修改配置文件路径</li>
</ol>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309090913982.png" alt="image-20230909091316944" style="zoom:80%;" />

<h2 id="3-2-Maven坐标详解"><a href="#3-2-Maven坐标详解" class="headerlink" title="3.2 Maven坐标详解"></a>3.2 Maven坐标详解</h2><ul>
<li><strong>什么是坐标？</strong></li>
</ul>
<p>​	Maven 中的坐标是&#x3D;&#x3D;资源的唯一标识&#x3D;&#x3D; </p>
<p>​	使用坐标来定义项目或引入项目中需要的依赖</p>
<ul>
<li><strong>Maven坐标主要组成</strong></li>
</ul>
<p>​	groupld：定义当前Maven项目隶属组织名称 (通常是域名反写，例如: com.itheima)</p>
<p>​	artifactld：定义当前Maven项目名称 (通常是模块名称，例如 order-service、goods-service)</p>
<p>​	version：定义当前项目版本号</p>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309090915607.png" alt="image-20230909091556578" style="zoom:80%;" />

<h2 id="3-3-IDEA创建Maven项目"><a href="#3-3-IDEA创建Maven项目" class="headerlink" title="3.3 IDEA创建Maven项目"></a>3.3 IDEA创建Maven项目</h2><ol>
<li>创建模块，选择Maven，点击Next</li>
</ol>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309091132685.png" alt="image-20230909113206599" style="zoom:80%;" />

<ol start="2">
<li>填写模块名称，坐标信息，点击finish，创建完成</li>
</ol>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309091132616.png" alt="image-20230909113254582" style="zoom:80%;" />

<ol start="3">
<li>编写 HelloWorld，并运行</li>
</ol>
<blockquote>
<p>注意：project和module中所有东西的版本号要保持高度一致</p>
</blockquote>
<h2 id="3-4-IDEA导入Maven项目"><a href="#3-4-IDEA导入Maven项目" class="headerlink" title="3.4 IDEA导入Maven项目"></a>3.4 IDEA导入Maven项目</h2><ol>
<li><p>选择右侧Maven面板，点击 +号</p>
</li>
<li><p>选中对应项目的pom.xml文件，双击即可</p>
</li>
<li><p>如果没有Maven面板，选择 View &gt; Appearance &gt; Tool Window Bars</p>
</li>
</ol>
<h3 id="3-4-1-配置-Maven-Helper-插件"><a href="#3-4-1-配置-Maven-Helper-插件" class="headerlink" title="3.4.1 配置 Maven-Helper 插件"></a>3.4.1 配置 Maven-Helper 插件</h3><ol>
<li>选择IDEA中 File –&gt; Settings</li>
<li>选择 Plugins</li>
<li>搜索Maven，选择第一个 Maven Helper，点击Install安装，弹出面板中点击Accept</li>
<li>重启IDEA</li>
</ol>
<h1 id="4-依赖管理"><a href="#4-依赖管理" class="headerlink" title="4. 依赖管理"></a>4. 依赖管理</h1><h2 id="4-1-使用坐标导入jar包"><a href="#4-1-使用坐标导入jar包" class="headerlink" title="4.1 使用坐标导入jar包"></a>4.1 使用坐标导入jar包</h2><p>1.在 pom.xml 中编写 <code>&lt;dependencies&gt;</code> 标签</p>
<p>2.在 <code>&lt;dependencies&gt;</code> 标签中 使用 <code>&lt;dependency&gt;</code> 引入坐标</p>
<p>3.定义坐标的 &#x3D;&#x3D;groupld，artifactld，version&#x3D;&#x3D;</p>
<p>4.点击刷新按钮，使坐标生效</p>
<h2 id="4-2-依赖范围"><a href="#4-2-依赖范围" class="headerlink" title="4.2 依赖范围"></a>4.2 依赖范围</h2><p>通过设置坐标的依赖范围(scope)，可以设置对应iar包的作用范围:编译环境、测试环境、运行环境</p>
<img src="https://csh-picture.oss-cn-shenzhen.aliyuncs.com/img202309091635253.png" alt="image-20230909163549217" style="zoom:80%;" />

<table>
<thead>
<tr>
<th>依赖范围</th>
<th>编译classpath</th>
<th>测试classpath</th>
<th>运行classpath</th>
<th>例子</th>
</tr>
</thead>
<tbody><tr>
<td>compile</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>logback</td>
</tr>
<tr>
<td>test</td>
<td>-</td>
<td>Y</td>
<td>-</td>
<td>Junit</td>
</tr>
<tr>
<td>provided</td>
<td>Y</td>
<td>Y</td>
<td>-</td>
<td>servlet-api</td>
</tr>
<tr>
<td>runtime</td>
<td>-</td>
<td>Y</td>
<td>Y</td>
<td>jdbc驱动</td>
</tr>
<tr>
<td>system</td>
<td>Y</td>
<td>Y</td>
<td>-</td>
<td>存储在本地的jar包</td>
</tr>
<tr>
<td>import</td>
<td>引入DependencyManagement</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<blockquote>
<p><scope>默认值：compile</p>
</blockquote>

    </div>

    
    <div class="about">
        <h1>About this Post</h1>
        <div class="details">
            <p>This post is written by John Doe, licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc/4.0">CC BY-NC 4.0</a>.</p>
        </div>
        
        <p class="tags text-capitalize">
            
            <i class="icon"></i>
            <a href="/tags/Java-Web/" class="tag">#Java Web</a>
        </p>
        
    </div>
    

    <div class="container post-prev-next">
        <a class="next"></a>
        
        <a href="/2023/09/21/day03-JDBC/" class="prev">
            <div>
                <div class="text">
                    <p class="label">Previous</p>
                    <h3 class="title">day03-JDBC</>
                </div>
            </div>
        </a>
        
    </div>

    
        
        
    
</article>

        <footer>
    <div class="inner">
        <div class="links">
            
            <div class="group">
                <h2 class="title">Blog</h2>
                
                <a href="/" class="item">Blog</a>
                
                <a href="/archives" class="item">Archives</a>
                
                <a href="/tags" class="item">Tags</a>
                
                <a href="/categories" class="item">Categories</a>
                
                <a href="/about" class="item">About</a>
                
            </div>
            
            <div class="group">
                <h2 class="title">Projects</h2>
                
                <a target="_blank" rel="noopener" href="https://github.com/MrWillCom/rsa-cli" class="item">RSA CLI</a>
                
                <a target="_blank" rel="noopener" href="https://github.com/MrWillCom/hexo-theme-cupertino" class="item">Hexo Theme Cupertino</a>
                
                <a target="_blank" rel="noopener" href="https://github.com/MrWillCom/a-calendar" class="item">A Calendar</a>
                
                <a target="_blank" rel="noopener" href="https://github.com/MrWillCom/auto-mirroring-bucket" class="item">Auto Mirroring Bucket</a>
                
            </div>
            
            <div class="group">
                <h2 class="title">Me</h2>
                
                <a target="_blank" rel="noopener" href="https://github.com/Satellite2426" class="item">GitHub</a>
                
                <a href="mailto:chaoshihan2426@gmail.com" class="item">Email</a>
                
            </div>
            
        </div>
        <span>&copy; 2023 John Doe<br>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> </span>
        
        
            <br>
            <div class="color-scheme-toggle" role="radiogroup" id="theme-color-scheme-toggle">
                <label>
                    <input type="radio" value="light">
                    <span>Light</span>
                </label>
                <label>
                    <input type="radio" value="dark">
                    <span>Dark</span>
                </label>
                <label>
                    <input type="radio" value="auto">
                    <span>Auto</span>
                </label>
            </div>
        
    </div>
</footer>


        
<script src="/js/main.js"></script>

        
        
        

        
        <script src="https://unpkg.com/scrollreveal"></script>
        <script>
            window.addEventListener('load', () => {
                ScrollReveal({ delay: 250, reset: true, easing: 'cubic-bezier(0, 0, 0, 1)' })
                ScrollReveal().reveal('.post-list-item .cover-img img')
                ScrollReveal().reveal('.post-list-item, .card, .content p img, .content .block-large img', { distance: '60px', origin: 'bottom', duration: 800 })
            })
        </script>
        
    </body>
</html>